# encoding: utf-8

import os
from glob import glob
from urllib.parse import quote as url_quote
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.responses import StreamingResponse
from fastapi.responses import FileResponse
from fastapi.templating import Jinja2Templates

from fastapi.responses import RedirectResponse

import uvicorn

app = FastAPI()
templates = Jinja2Templates(directory="templates")


@app.get("/")
def index(request: Request):
    return RedirectResponse("/home", status_code=302)


@app.get("/home")
def home(request: Request):
    # mp4_list = []
    # file_path = r"D:/BaiduNetdiskDownload/9dm_ELDEN_RING/*"
    file_path = r"E:/data/temp/*"
    file_list = glob(file_path)
    file_dict = {}
    for x in file_list:
        file_dict[x] = url_quote(x)
    total = len(file_list)
    # print(mp4_list[:10])
    return templates.TemplateResponse("share.html", {"request": request, "file_dict": file_dict, "total": total})


@app.get("/download/")
def download_vidoe(file_path: str):

    file_name = os.path.basename(file_path)
    return FileResponse(file_path, filename=file_name)
    # return StreamingResponse(file_path)


if __name__ == '__main__':
    uvicorn.run(app, host="192.168.1.100", port=1237, access_log=True)
